package com.xique.blelock.view.AcountManager;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.clj.fastble.utils.HexUtil;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.xique.blelock.Interface.BleListenData;
import com.xique.blelock.R;
import com.xique.blelock.Utils.ACache;
import com.xique.blelock.Utils.BleUtils;
import com.xique.blelock.Utils.Stringkey;
import com.xique.blelock.model.LockAllinfo;
import com.xique.blelock.model.UserRole;
import com.xique.blelock.view.BaseActivity;
import com.xique.blelock.view.DeviceManager.DeviceControllerActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/12/15.
 */

public class QuickSettingActivty extends BaseActivity implements BleListenData {

    @BindView(R.id.title)
    TextView title;

    @BindView(R.id.fastopenlock)
    ToggleButton fastopenlock;

    @BindView(R.id.universeropen)
    ToggleButton universeropen;

    @BindView(R.id.managerlayout)
    View managerlayout;

    private boolean fastopen;
    private boolean unveropen;

    private LockAllinfo lockAllinfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quicksettinglayout);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        BleUtils.setBleListen(this);
    }

    @Override
    protected void initView() {
        super.initView();

        title.setText("设备设置");

        lockAllinfo = (LockAllinfo) ACache.get(this).getAsObject(Stringkey.lockAllinfo);
        Boolean faseopen = (Boolean) ACache.get(this).getAsObject(Stringkey.fast_open_prefix+BleUtils.getBleDevice().getMac().replace(":",""));
        if(faseopen != null)
        {
            fastopenlock.setChecked(faseopen);
        }

        universeropen.setChecked(lockAllinfo.isIsusualopen());


        if(lockAllinfo.getRole() == UserRole.commuser){
            managerlayout.setVisibility(View.GONE);
        }else{
            managerlayout.setVisibility(View.VISIBLE);
        }



    }

    @OnClick(R.id.BACKER)
    public void BackLastPage(){
        finish();
    }

    @OnClick({R.id.fastopenlock, R.id.universeropen})
    public void settingOnclick(View view){
        if(view.getId() == R.id.fastopenlock){
            //这里先发给设备
            ACache.get(this).put(Stringkey.fast_open_prefix+BleUtils.getBleDevice().getMac().replace(":",""), new Boolean(fastopenlock.isChecked()));
        }if(view.getId() == R.id.universeropen){
            if(universeropen.isChecked())
            {
                BleUtils.sendData(Stringkey.comm_open_mode);
            }else{
                BleUtils.sendData(Stringkey.comm_close_mode);
            }
        }
    }


    @OnClick({R.id.factorybtn, R.id.rebootdev})
    public void factoryclick(View view){
        if(userInfo.getRole() != 0){
            showToast(this, "没有权限操作");
            return;
        }
        if(view.getId() == R.id.factorybtn)
        {
            tipFactoryDialog();
        }else{

            tipReboot();
        }

    }

    private void tipReboot() {
             new MaterialDialog.Builder(this)
                .title("提示")// 标题
                .titleColor(getResources().getColor(R.color.tipstitle))
                .content("确定要重启吗，请谨慎操作！")// 内容
                .contentColor(Color.BLACK)// 内容颜色
                .positiveText("确定")
                .positiveColorRes(R.color.cologbtnhou)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        dialog.dismiss();
                        BleUtils.sendData(Stringkey.rebootdev);
                    }
                })
                .negativeText("取消")
                .negativeColorRes(R.color.cancle)
                .show();

    }

    private void tipFactoryDialog() {
              new MaterialDialog.Builder(this)
                .title("提示")// 标题
                .titleColor(getResources().getColor(R.color.tipstitle))
                .content("恢复出厂设置后所有的数据将被全部清空，请谨慎操作！")// 内容
                .contentColor(Color.BLACK)// 内容颜色
                .positiveText("确定")
                .positiveColorRes(R.color.cologbtnhou)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        dialog.dismiss();
                        BleUtils.sendData(Stringkey.factorydev);
                    }
                })
                .negativeText("取消")
                .negativeColorRes(R.color.cancle)
                .show();
    }

    @Override
    public void didReciveData(byte[] data) {
        String rep = HexUtil.encodeHexStr(data);
        if(rep.equals(Stringkey.factory_ok)){
            showToast(this, "正在恢复出厂设置");
        }

        if(rep.equals(Stringkey.factory_fail)){
            showToast(this, "恢复出厂设置失败");
        }
        else if(rep.startsWith(Stringkey.rebootdev)){
            showToast(this, " 设备正在重启");
        }
        //
        else if(rep.startsWith(Stringkey.comm_open_mode_rep_ok)){//设置长开模式
            showToast(this, "设置常开模式");
            universeropen.setChecked(true);
            lockAllinfo.setIsusualopen(true);
            ACache.get(this).put(Stringkey.lockAllinfo, lockAllinfo);

        }else if(rep.startsWith(Stringkey.comm_close_mode_rep_ok)){//取消长开模式
            showToast(this, "取消常开模式");
            universeropen.setChecked(false);
            lockAllinfo.setIsusualopen(false);
            ACache.get(this).put(Stringkey.lockAllinfo, lockAllinfo);
        }
    }
}
